package cn.tkb.user.controller;
import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.PageQuery;
import cn.tkb.user.domain.LoginInfo;
import cn.tkb.user.query.LoginInfoQuery;
import cn.tkb.user.service.ILoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/loginInfo")
public class LoginInfoController {
    @Autowired
    private ILoginInfoService loginInfoService;
    //新增和修改
    @RequestMapping
    public AjaxResult addOrUpdate(@RequestBody LoginInfo loginInfo){
        try {
            if (loginInfo.getId() == null){
                loginInfoService.add(loginInfo);
            }else {
                loginInfoService.update(loginInfo);
            }
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！！！");
        }
    }
    //查询登录信息根据id
    @GetMapping("/{id}")
    public LoginInfo queryById(@PathVariable("id") Long id){
        return loginInfoService.QeuryById(id);
    }
    //删除登录信息
    @DeleteMapping("/{id}")
        public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            loginInfoService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }

    }
    //批量删除
    @PatchMapping
    public AjaxResult BatchDelete(@RequestBody List<Long> ids){
        try {
            loginInfoService.BatchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail(e.getMessage());
        }
    }
    //查询所有登录信息
    @GetMapping
    public List<LoginInfo> queryAll(){
        return loginInfoService.QeuryAll();
    }
    //高级查询和分页查询
    @PostMapping
    public PageQuery<LoginInfo> queryPage(@RequestBody LoginInfoQuery query){
       return loginInfoService.findPage(query) ;
    }
}
